/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.business.rest;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import me.zhengjie.annotation.Log;
import me.zhengjie.business.domain.Jsxx;
import me.zhengjie.business.domain.sup.Gsgl;
import me.zhengjie.business.service.BisJsxxService;
import me.zhengjie.business.service.GsglService;
import me.zhengjie.business.service.dto.BisJsxxQueryCriteria;
import me.zhengjie.utils.DateTimeUtil;
import me.zhengjie.utils.SecurityUtils;
import org.springframework.data.domain.Pageable;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

/**
* @website https://el-admin.vip
* @author xiahongbo
* @date 2023-02-13
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "结算信息管理")
@RequestMapping("/api/bisJsxx")
public class BisJsxxController {

    private final BisJsxxService bisJsxxService;

    private final GsglService gsglService;

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('bisJsxx:list')")
    public void exportBisJsxx(HttpServletResponse response, BisJsxxQueryCriteria criteria) throws IOException {
        bisJsxxService.download(bisJsxxService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询结算信息")
    @ApiOperation("查询结算信息")
    @PreAuthorize("@el.check('bisJsxx:list')")
    public ResponseEntity<Object> queryBisJsxx(BisJsxxQueryCriteria criteria, Pageable pageable){
        Gsgl gsgl = gsglService.queryGsgl(SecurityUtils.getCurrentUserId());
        criteria.setBisJsxxYljgbm(gsgl.getBisGsglYljgdm());
    /*    String jsrq = criteria.getJsrq();
        String start = jsrq + " 00:00:00";
        String end = jsrq + " 23:59:59";
        Date startJsrq = DateTimeUtil.parse(start, DateTimeUtil.DEFAULT_DATETIME_FORMAT);
        Date endJsrq = DateTimeUtil.parse(end, DateTimeUtil.DEFAULT_DATETIME_FORMAT);
        List<Date> list = new ArrayList();
        list.add(startJsrq);
        list.add(endJsrq);
        criteria.setBisJsxxJssj(list);*/
        return new ResponseEntity<>(bisJsxxService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增结算信息")
    @ApiOperation("新增结算信息")
    @PreAuthorize("@el.check('bisJsxx:add')")
    public ResponseEntity<Object> createBisJsxx(@Validated @RequestBody Jsxx resources){
        return new ResponseEntity<>(bisJsxxService.create(resources),HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改结算信息")
    @ApiOperation("修改结算信息")
    @PreAuthorize("@el.check('bisJsxx:edit')")
    public ResponseEntity<Object> updateBisJsxx(@Validated @RequestBody Jsxx resources){
        bisJsxxService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @DeleteMapping
    @Log("删除结算信息")
    @ApiOperation("删除结算信息")
    @PreAuthorize("@el.check('bisJsxx:del')")
    public ResponseEntity<Object> deleteBisJsxx(@RequestBody String[] ids) {
        bisJsxxService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }
}